// @Author EthanScriptOn
// @Desc
package result_collector

import (
	"fmt"
	"gitee.com/fatzeng/srf_switch_stream_bridge/bridge"
	"gitee.com/fatzeng/srf_switch_stream_bridge/framework/actuator"
)

type LoopBridgeResultCollector struct{}

func GenerateLoopBridgeResultCollector() *LoopBridgeResultCollector {
	return new(LoopBridgeResultCollector)
}

func (l *LoopBridgeResultCollector) CollectResult(ctx *actuator.SrfSwitchBridgeActuatorContext, config *bridge.BridgeConfig, response interface{}) (err error) {
	if response == nil {
		_ = ctx.Record(actuator.ResultCollection, fmt.Sprintf("response is nil"))
		return
	}
	loopResponse, hasResponse := ctx.GetResponse(config.MultiChannelDataOperatorIdentifier)
	if loopResponse == nil || !hasResponse {
		loopResponse = make(chan interface{})
		ctx.SaveResponse(config.MultiChannelDataOperatorIdentifier, loopResponse)
	}
	channel := loopResponse.(chan interface{})
	channel <- response
	return
}
